package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2019/7/9.
 */
@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IStrategyThemeService strategyThemeService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        strategyDetailService.delete(id);
        return new AjaxResult();
    }

    @RequestMapping("changeState")
    @ResponseBody
    public Object changeState(StrategyDetail strategyDetail) {
        strategyDetailService.changeState(strategyDetail);
        return new AjaxResult();
    }
    @RequestMapping("getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogService.getCatalogByStrategyId(strategyId);
    }
    @RequestMapping("input")
    public String input(Long id,Model model) {
        model.addAttribute("strategies",strategyService.listAll());
        model.addAttribute("themes",strategyThemeService.listAll());
        if (id==null){
            return "strategyDetail/input";
        }else{
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            //查询,应为是两张表,所以需要额外查询内容表,然后设置进去
            StrategyContent strategyContent = strategyContentService.get(id);
            strategyDetail.setStrategyContent(strategyContent);
            //这里我们也需要设置标签回显,需要将标签额外查询出来
            String tags = strategyTagService.getByDetailId(id);
            model.addAttribute("strategyDetail",strategyDetail);
            model.addAttribute("tags",tags);
            return "strategyDetail/input";
        }
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail,String tags) {
        strategyDetailService.saveOrUpdate(strategyDetail,tags);
        return new AjaxResult();
    }
}
